{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from deap import base, creator, tools, gp, algorithms\n",
    "import math\n",
    "import random\n",
    "import operator\n",
    "import time\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "COL_NUM = 0\n",
    "ROW_NUM = 0\n",
    "\n",
    "\n",
    "def _protected_division(x1, x2):\n",
    "    with np.errstate(divide=\"ignore\", invalid=\"ignore\"):\n",
    "        return np.where(np.abs(x2) > 1e-10, np.divide(x1, x2), 1.)\n",
    "\n",
    "\n",
    "def _protected_log(x1):\n",
    "    with np.errstate(divide=\"ignore\", invalid=\"ignore\"):\n",
    "        return np.where(np.abs(x1) > 1e-10, np.log(np.abs(x1)), 0.0)\n",
    "\n",
    "\n",
    "def _protected_sqrt(x1):\n",
    "    return np.sqrt(np.abs(x1))\n",
    "\n",
    "\n",
    "def _protected_inverse(x1):\n",
    "    with np.errstate(divide=\"ignore\", invalid=\"ignore\"):\n",
    "        return np.where(np.abs(x1) > 1e-10, 1.0 / x1, 0.)\n",
    "\n",
    "\n",
    "def _sigmoid(x1):\n",
    "    with np.errstate(over=\"ignore\", under=\"ignore\"):\n",
    "        return 1. / (1 + np.exp(-x1))\n",
    "\n",
    "\n",
    "def gp_add(x, y):\n",
    "    \"\"\"\n",
    "    第一个函数\n",
    "    :param x:\n",
    "    :param y:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return operator.add(x, y)\n",
    "\n",
    "\n",
    "def gp_sub(x, y):\n",
    "    \"\"\"\n",
    "    第二个函数\n",
    "    :param x:\n",
    "    :param y:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return operator.sub(x, y)\n",
    "\n",
    "\n",
    "def gp_mul(x, y):\n",
    "    \"\"\"\n",
    "    第三个函数\n",
    "    :param x:\n",
    "    :param y:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return operator.mul(x, y)\n",
    "\n",
    "\n",
    "def gp_div(x, y):\n",
    "    \"\"\"\n",
    "    第四个函数\n",
    "    :param x:\n",
    "    :param y:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return _protected_division(x, y)\n",
    "\n",
    "\n",
    "def gp_sqrt(data):\n",
    "    \"\"\"\n",
    "    第五个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return _protected_sqrt(data)\n",
    "\n",
    "\n",
    "def gp_log(data):\n",
    "    \"\"\"\n",
    "    第六个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return _protected_log(data)\n",
    "\n",
    "\n",
    "def gp_neg(data):\n",
    "    \"\"\"\n",
    "    第七个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return np.negative(data)\n",
    "\n",
    "\n",
    "def gp_inv(data):\n",
    "    \"\"\"\n",
    "    第八个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return _protected_inverse(data)\n",
    "\n",
    "\n",
    "def gp_abs(data):\n",
    "    \"\"\"\n",
    "    第九个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return np.abs(data)\n",
    "\n",
    "\n",
    "def gp_sin(data):\n",
    "    \"\"\"\n",
    "    第十个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return np.sin(data)\n",
    "\n",
    "\n",
    "def gp_cos(data):\n",
    "    \"\"\"\n",
    "    第十一个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return np.cos(data)\n",
    "\n",
    "\n",
    "def gp_tan(data):\n",
    "    \"\"\"\n",
    "    第十二个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return np.tan(data)\n",
    "\n",
    "\n",
    "def gp_sigmoid(data):\n",
    "    \"\"\"\n",
    "    第十三个函数\n",
    "    :param data:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    return _sigmoid(data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def init_data():\n",
    "    start_time = time.time()\n",
    "    with open(\"model_data.csv\") as f:\n",
    "        raw_data = pd.read_csv(f, sep=\",\", index_col=\"Timestamp\", na_values=['#VALUE!', '#DIV/0!'])\n",
    "    raw_data[\"AvgPrice\"] = gp_div(raw_data[\"val\"], raw_data[\"vol\"])\n",
    "    raw_data1 = raw_data.iloc[:, [0, 8, 9, -1]]\n",
    "    raw_data2 = raw_data.iloc[:, 11:31]\n",
    "\n",
    "    # 尝试找出魏老师的公式，哈哈哈\n",
    "    y_data = raw_data.iloc[:, -2]\n",
    "\n",
    "    data = pd.concat([raw_data1, raw_data2], axis=1)\n",
    "    VARIABLES = data.columns\n",
    "    index_col = data.index\n",
    "    global ROW_NUM, COL_NUM\n",
    "    ROW_NUM, COL_NUM = data.shape\n",
    "    print(\"variables: \", VARIABLES)\n",
    "    data = data.values\n",
    "    data = data.astype(np.float64)\n",
    "    data = pd.DataFrame(data, columns=VARIABLES, index=index_col)\n",
    "    end_time = time.time()\n",
    "    print(\"total_load_data_time: \", end_time - start_time)\n",
    "    return data, y_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "variables:  Index(['LastPrice', 'vol', 'val', 'AvgPrice', 'bp1', 'bv1', 'ap1', 'av1',\n",
      "       'bp2', 'bv2', 'ap2', 'av2', 'bp3', 'bv3', 'ap3', 'av3', 'bp4', 'bv4',\n",
      "       'ap4', 'av4', 'bp5', 'bv5', 'ap5', 'av5'],\n",
      "      dtype='object')\n",
      "total_load_data_time:  3.2581863403320312\n"
     ]
    }
   ],
   "source": [
    "arr_data, y_data = init_data()\n",
    "pset = gp.PrimitiveSet(\"Main\", arity=COL_NUM, prefix=\"var\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pset.addPrimitive(gp_add, arity=2, name=\"gp_add\")\n",
    "pset.addPrimitive(gp_sub, arity=2, name=\"gp_sub\")\n",
    "pset.addPrimitive(gp_mul, arity=2, name=\"gp_mul\")\n",
    "pset.addPrimitive(gp_div, arity=2, name=\"gp_div\")\n",
    "\n",
    "pset.addPrimitive(gp_tan, arity=1, name=\"gp_tan\")\n",
    "pset.addPrimitive(gp_sin, arity=1, name=\"gp_sin\")\n",
    "pset.addPrimitive(gp_cos, arity=1, name=\"gp_cos\")\n",
    "\n",
    "pset.addPrimitive(gp_abs, arity=1, name=\"gp_abs\")\n",
    "pset.addPrimitive(gp_inv, arity=1, name=\"gp_inv\")\n",
    "pset.addPrimitive(gp_neg, arity=1, name=\"gp_neg\")\n",
    "pset.addPrimitive(gp_log, arity=1, name=\"gp_log\")\n",
    "pset.addPrimitive(gp_sqrt, arity=1, name=\"gp_sqrt\")\n",
    "pset.addPrimitive(gp_sigmoid, arity=1, name=\"gp_sigmoid\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "creator.create(\"FitnessMin\", base.Fitness, weights=(-1.0, ))\n",
    "creator.create(\"Individual\", gp.PrimitiveTree, fitness=creator.FitnessMin)\n",
    "\n",
    "toolbox = base.Toolbox()\n",
    "toolbox.register(\"expr\", gp.genHalfAndHalf, pset=pset, min_=1, max_=3)\n",
    "toolbox.register(\"Individual\", tools.initIterate, creator.Individual, toolbox.expr)\n",
    "toolbox.register(\"population\", tools.initRepeat, list, toolbox.Individual)\n",
    "toolbox.register(\"compile\", gp.compile, pset=pset)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LastPrice</th>\n",
       "      <th>vol</th>\n",
       "      <th>val</th>\n",
       "      <th>AvgPrice</th>\n",
       "      <th>bp1</th>\n",
       "      <th>bv1</th>\n",
       "      <th>ap1</th>\n",
       "      <th>av1</th>\n",
       "      <th>bp2</th>\n",
       "      <th>bv2</th>\n",
       "      <th>...</th>\n",
       "      <th>ap3</th>\n",
       "      <th>av3</th>\n",
       "      <th>bp4</th>\n",
       "      <th>bv4</th>\n",
       "      <th>ap4</th>\n",
       "      <th>av4</th>\n",
       "      <th>bp5</th>\n",
       "      <th>bv5</th>\n",
       "      <th>ap5</th>\n",
       "      <th>av5</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>00:00.5</th>\n",
       "      <td>135270.0</td>\n",
       "      <td>132.0</td>\n",
       "      <td>17835500.0</td>\n",
       "      <td>135117.424242</td>\n",
       "      <td>135150.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135130.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135290.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135100.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>135300.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>135070.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135310.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:01.0</th>\n",
       "      <td>135230.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>5814350.0</td>\n",
       "      <td>135217.441860</td>\n",
       "      <td>135230.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>135280.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135300.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>135200.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135310.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>135320.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:01.5</th>\n",
       "      <td>135210.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2704560.0</td>\n",
       "      <td>135228.000000</td>\n",
       "      <td>135210.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135230.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>135200.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135260.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135180.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>135270.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>135170.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135280.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:02.0</th>\n",
       "      <td>135220.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>7166210.0</td>\n",
       "      <td>135211.509434</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>135180.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135240.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135150.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135250.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135140.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135260.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:02.5</th>\n",
       "      <td>135190.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>4461790.0</td>\n",
       "      <td>135205.757576</td>\n",
       "      <td>135140.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135160.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135120.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135200.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135100.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>135210.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135070.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           LastPrice    vol         val       AvgPrice       bp1   bv1  \\\n",
       "Timestamp                                                                \n",
       "00:00.5     135270.0  132.0  17835500.0  135117.424242  135150.0  27.0   \n",
       "00:01.0     135230.0   43.0   5814350.0  135217.441860  135230.0   7.0   \n",
       "00:01.5     135210.0   20.0   2704560.0  135228.000000  135210.0   9.0   \n",
       "00:02.0     135220.0   53.0   7166210.0  135211.509434  135190.0   1.0   \n",
       "00:02.5     135190.0   33.0   4461790.0  135205.757576  135140.0   1.0   \n",
       "\n",
       "                ap1  av1       bp2  bv2  ...       ap3   av3       bp4   bv4  \\\n",
       "Timestamp                                ...                                   \n",
       "00:00.5    135190.0  2.0  135130.0  9.0  ...  135290.0   1.0  135100.0   6.0   \n",
       "00:01.0    135280.0  8.0  135220.0  1.0  ...  135300.0  15.0  135200.0   1.0   \n",
       "00:01.5    135230.0  6.0  135200.0  2.0  ...  135260.0   2.0  135180.0   3.0   \n",
       "00:02.0    135220.0  3.0  135180.0  1.0  ...  135240.0   1.0  135150.0   4.0   \n",
       "00:02.5    135160.0  1.0  135120.0  3.0  ...  135200.0   1.0  135100.0  11.0   \n",
       "\n",
       "                ap4   av4       bp5  bv5       ap5   av5  \n",
       "Timestamp                                                 \n",
       "00:00.5    135300.0  15.0  135070.0  1.0  135310.0   1.0  \n",
       "00:01.0    135310.0   1.0  135190.0  7.0  135320.0   3.0  \n",
       "00:01.5    135270.0  19.0  135170.0  1.0  135280.0  12.0  \n",
       "00:02.0    135250.0   2.0  135140.0  1.0  135260.0   6.0  \n",
       "00:02.5    135210.0   2.0  135070.0  2.0  135220.0   5.0  \n",
       "\n",
       "[5 rows x 24 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "ret_arr = arr_data[\"LastPrice\"] / arr_data[\"LastPrice\"].shift(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "test_data = copy.deepcopy(arr_data[: 1000])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LastPrice</th>\n",
       "      <th>vol</th>\n",
       "      <th>val</th>\n",
       "      <th>AvgPrice</th>\n",
       "      <th>bp1</th>\n",
       "      <th>bv1</th>\n",
       "      <th>ap1</th>\n",
       "      <th>av1</th>\n",
       "      <th>bp2</th>\n",
       "      <th>bv2</th>\n",
       "      <th>...</th>\n",
       "      <th>ap3</th>\n",
       "      <th>av3</th>\n",
       "      <th>bp4</th>\n",
       "      <th>bv4</th>\n",
       "      <th>ap4</th>\n",
       "      <th>av4</th>\n",
       "      <th>bp5</th>\n",
       "      <th>bv5</th>\n",
       "      <th>ap5</th>\n",
       "      <th>av5</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>00:00.5</th>\n",
       "      <td>135270.0</td>\n",
       "      <td>132.0</td>\n",
       "      <td>17835500.0</td>\n",
       "      <td>135117.424242</td>\n",
       "      <td>135150.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135130.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135290.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135100.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>135300.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>135070.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135310.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:01.0</th>\n",
       "      <td>135230.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>5814350.0</td>\n",
       "      <td>135217.441860</td>\n",
       "      <td>135230.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>135280.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135300.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>135200.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135310.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>135320.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:01.5</th>\n",
       "      <td>135210.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2704560.0</td>\n",
       "      <td>135228.000000</td>\n",
       "      <td>135210.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135230.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>135200.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135260.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135180.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>135270.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>135170.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135280.0</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:02.0</th>\n",
       "      <td>135220.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>7166210.0</td>\n",
       "      <td>135211.509434</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>135180.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135240.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135150.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135250.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135140.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135260.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:02.5</th>\n",
       "      <td>135190.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>4461790.0</td>\n",
       "      <td>135205.757576</td>\n",
       "      <td>135140.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135160.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135120.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135200.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135100.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>135210.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135070.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:18.5</th>\n",
       "      <td>135520.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>677560.0</td>\n",
       "      <td>135512.000000</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135520.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>135490.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135540.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>135470.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135550.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135460.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135560.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:19.0</th>\n",
       "      <td>135500.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>271010.0</td>\n",
       "      <td>135505.000000</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>135510.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135490.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135530.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>135470.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135540.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>135460.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135550.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:19.5</th>\n",
       "      <td>135510.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>4472130.0</td>\n",
       "      <td>135519.090909</td>\n",
       "      <td>135510.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>135530.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135550.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135480.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>135560.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135470.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135570.0</td>\n",
       "      <td>28.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:20.0</th>\n",
       "      <td>135560.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>6641640.0</td>\n",
       "      <td>135543.673469</td>\n",
       "      <td>135540.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135570.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>135530.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135590.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135510.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>135600.0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>135610.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:20.5</th>\n",
       "      <td>135530.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1761980.0</td>\n",
       "      <td>135536.923077</td>\n",
       "      <td>135530.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135560.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135520.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>...</td>\n",
       "      <td>135580.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>135590.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135490.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135600.0</td>\n",
       "      <td>66.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 24 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           LastPrice    vol         val       AvgPrice       bp1   bv1  \\\n",
       "Timestamp                                                                \n",
       "00:00.5     135270.0  132.0  17835500.0  135117.424242  135150.0  27.0   \n",
       "00:01.0     135230.0   43.0   5814350.0  135217.441860  135230.0   7.0   \n",
       "00:01.5     135210.0   20.0   2704560.0  135228.000000  135210.0   9.0   \n",
       "00:02.0     135220.0   53.0   7166210.0  135211.509434  135190.0   1.0   \n",
       "00:02.5     135190.0   33.0   4461790.0  135205.757576  135140.0   1.0   \n",
       "...              ...    ...         ...            ...       ...   ...   \n",
       "08:18.5     135520.0    5.0    677560.0  135512.000000  135500.0   2.0   \n",
       "08:19.0     135500.0    2.0    271010.0  135505.000000  135500.0  22.0   \n",
       "08:19.5     135510.0   33.0   4472130.0  135519.090909  135510.0  48.0   \n",
       "08:20.0     135560.0   49.0   6641640.0  135543.673469  135540.0   2.0   \n",
       "08:20.5     135530.0   13.0   1761980.0  135536.923077  135530.0   4.0   \n",
       "\n",
       "                ap1   av1       bp2   bv2  ...       ap3   av3       bp4  \\\n",
       "Timestamp                                  ...                             \n",
       "00:00.5    135190.0   2.0  135130.0   9.0  ...  135290.0   1.0  135100.0   \n",
       "00:01.0    135280.0   8.0  135220.0   1.0  ...  135300.0  15.0  135200.0   \n",
       "00:01.5    135230.0   6.0  135200.0   2.0  ...  135260.0   2.0  135180.0   \n",
       "00:02.0    135220.0   3.0  135180.0   1.0  ...  135240.0   1.0  135150.0   \n",
       "00:02.5    135160.0   1.0  135120.0   3.0  ...  135200.0   1.0  135100.0   \n",
       "...             ...   ...       ...   ...  ...       ...   ...       ...   \n",
       "08:18.5    135520.0   3.0  135490.0   7.0  ...  135540.0  14.0  135470.0   \n",
       "08:19.0    135510.0   1.0  135490.0   7.0  ...  135530.0  14.0  135470.0   \n",
       "08:19.5    135530.0  12.0  135500.0  25.0  ...  135550.0   9.0  135480.0   \n",
       "08:20.0    135570.0  28.0  135530.0   5.0  ...  135590.0   4.0  135510.0   \n",
       "08:20.5    135560.0   4.0  135520.0  20.0  ...  135580.0   6.0  135500.0   \n",
       "\n",
       "            bv4       ap4   av4       bp5   bv5       ap5   av5  \n",
       "Timestamp                                                        \n",
       "00:00.5     6.0  135300.0  15.0  135070.0   1.0  135310.0   1.0  \n",
       "00:01.0     1.0  135310.0   1.0  135190.0   7.0  135320.0   3.0  \n",
       "00:01.5     3.0  135270.0  19.0  135170.0   1.0  135280.0  12.0  \n",
       "00:02.0     4.0  135250.0   2.0  135140.0   1.0  135260.0   6.0  \n",
       "00:02.5    11.0  135210.0   2.0  135070.0   2.0  135220.0   5.0  \n",
       "...         ...       ...   ...       ...   ...       ...   ...  \n",
       "08:18.5     8.0  135550.0   9.0  135460.0   9.0  135560.0   4.0  \n",
       "08:19.0     8.0  135540.0  10.0  135460.0   9.0  135550.0   9.0  \n",
       "08:19.5     5.0  135560.0   4.0  135470.0   8.0  135570.0  28.0  \n",
       "08:20.0    19.0  135600.0  66.0  135500.0  25.0  135610.0   6.0  \n",
       "08:20.5    25.0  135590.0   9.0  135490.0   9.0  135600.0  66.0  \n",
       "\n",
       "[1000 rows x 24 columns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LastPrice    1.000000\n",
       "vol          0.228079\n",
       "val          0.228676\n",
       "AvgPrice     0.092763\n",
       "bp1          0.995153\n",
       "bv1          0.155783\n",
       "ap1          0.995390\n",
       "av1          0.132528\n",
       "bp2          0.994771\n",
       "bv2          0.135473\n",
       "ap2          0.995769\n",
       "av2          0.159595\n",
       "bp3          0.994171\n",
       "bv3          0.182213\n",
       "ap3          0.995388\n",
       "av3          0.142658\n",
       "bp4          0.994051\n",
       "bv4          0.142280\n",
       "ap4          0.995302\n",
       "av4          0.130809\n",
       "bp5          0.993327\n",
       "bv5          0.175639\n",
       "ap5          0.995198\n",
       "av5          0.100770\n",
       "dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data.corrwith(test_data[\"LastPrice\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "temp_arr_data = copy.deepcopy(arr_data[: 1000])\n",
    "# 计算 10个tick 后的收益率\n",
    "temp_arr_data[\"ret_value10\"] = _protected_division(temp_arr_data[\"LastPrice\"],\n",
    "                                                   temp_arr_data[\"LastPrice\"].shift(10)) - 1.\n",
    "# 计算 20个tick 后的收益率\n",
    "temp_arr_data[\"ret_value20\"] = _protected_division(temp_arr_data[\"LastPrice\"],\n",
    "                                                   temp_arr_data[\"LastPrice\"].shift(20)) - 1.\n",
    "# 计算 30个tick 后的收益率\n",
    "temp_arr_data[\"ret_value30\"] = _protected_division(temp_arr_data[\"LastPrice\"],\n",
    "                                                   temp_arr_data[\"LastPrice\"].shift(30)) - 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LastPrice</th>\n",
       "      <th>vol</th>\n",
       "      <th>val</th>\n",
       "      <th>AvgPrice</th>\n",
       "      <th>bp1</th>\n",
       "      <th>bv1</th>\n",
       "      <th>ap1</th>\n",
       "      <th>av1</th>\n",
       "      <th>bp2</th>\n",
       "      <th>bv2</th>\n",
       "      <th>...</th>\n",
       "      <th>bv4</th>\n",
       "      <th>ap4</th>\n",
       "      <th>av4</th>\n",
       "      <th>bp5</th>\n",
       "      <th>bv5</th>\n",
       "      <th>ap5</th>\n",
       "      <th>av5</th>\n",
       "      <th>ret_value10</th>\n",
       "      <th>ret_value20</th>\n",
       "      <th>ret_value30</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Timestamp</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>00:00.5</th>\n",
       "      <td>135270.0</td>\n",
       "      <td>132.0</td>\n",
       "      <td>17835500.0</td>\n",
       "      <td>135117.424242</td>\n",
       "      <td>135150.0</td>\n",
       "      <td>27.0</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135130.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>...</td>\n",
       "      <td>6.0</td>\n",
       "      <td>135300.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>135070.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135310.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:01.0</th>\n",
       "      <td>135230.0</td>\n",
       "      <td>43.0</td>\n",
       "      <td>5814350.0</td>\n",
       "      <td>135217.441860</td>\n",
       "      <td>135230.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>135280.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135310.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>135320.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:01.5</th>\n",
       "      <td>135210.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2704560.0</td>\n",
       "      <td>135228.000000</td>\n",
       "      <td>135210.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135230.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>135200.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>135270.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>135170.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135280.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:02.0</th>\n",
       "      <td>135220.0</td>\n",
       "      <td>53.0</td>\n",
       "      <td>7166210.0</td>\n",
       "      <td>135211.509434</td>\n",
       "      <td>135190.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>135180.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>...</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135250.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135140.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135260.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>00:02.5</th>\n",
       "      <td>135190.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>4461790.0</td>\n",
       "      <td>135205.757576</td>\n",
       "      <td>135140.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135160.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135120.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>...</td>\n",
       "      <td>11.0</td>\n",
       "      <td>135210.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135070.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135220.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:18.5</th>\n",
       "      <td>135520.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>677560.0</td>\n",
       "      <td>135512.000000</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135520.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>135490.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135550.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135460.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135560.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.000148</td>\n",
       "      <td>0.000074</td>\n",
       "      <td>-0.000148</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:19.0</th>\n",
       "      <td>135500.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>271010.0</td>\n",
       "      <td>135505.000000</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>22.0</td>\n",
       "      <td>135510.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>135490.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135540.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>135460.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135550.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.000074</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:19.5</th>\n",
       "      <td>135510.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>4472130.0</td>\n",
       "      <td>135519.090909</td>\n",
       "      <td>135510.0</td>\n",
       "      <td>48.0</td>\n",
       "      <td>135530.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>...</td>\n",
       "      <td>5.0</td>\n",
       "      <td>135560.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135470.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>135570.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>0.000221</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:20.0</th>\n",
       "      <td>135560.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>6641640.0</td>\n",
       "      <td>135543.673469</td>\n",
       "      <td>135540.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>135570.0</td>\n",
       "      <td>28.0</td>\n",
       "      <td>135530.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>...</td>\n",
       "      <td>19.0</td>\n",
       "      <td>135600.0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>135500.0</td>\n",
       "      <td>25.0</td>\n",
       "      <td>135610.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.000295</td>\n",
       "      <td>0.000590</td>\n",
       "      <td>0.000221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>08:20.5</th>\n",
       "      <td>135530.0</td>\n",
       "      <td>13.0</td>\n",
       "      <td>1761980.0</td>\n",
       "      <td>135536.923077</td>\n",
       "      <td>135530.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135560.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>135520.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>...</td>\n",
       "      <td>25.0</td>\n",
       "      <td>135590.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135490.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>135600.0</td>\n",
       "      <td>66.0</td>\n",
       "      <td>0.000148</td>\n",
       "      <td>0.000295</td>\n",
       "      <td>0.000074</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           LastPrice    vol         val       AvgPrice       bp1   bv1  \\\n",
       "Timestamp                                                                \n",
       "00:00.5     135270.0  132.0  17835500.0  135117.424242  135150.0  27.0   \n",
       "00:01.0     135230.0   43.0   5814350.0  135217.441860  135230.0   7.0   \n",
       "00:01.5     135210.0   20.0   2704560.0  135228.000000  135210.0   9.0   \n",
       "00:02.0     135220.0   53.0   7166210.0  135211.509434  135190.0   1.0   \n",
       "00:02.5     135190.0   33.0   4461790.0  135205.757576  135140.0   1.0   \n",
       "...              ...    ...         ...            ...       ...   ...   \n",
       "08:18.5     135520.0    5.0    677560.0  135512.000000  135500.0   2.0   \n",
       "08:19.0     135500.0    2.0    271010.0  135505.000000  135500.0  22.0   \n",
       "08:19.5     135510.0   33.0   4472130.0  135519.090909  135510.0  48.0   \n",
       "08:20.0     135560.0   49.0   6641640.0  135543.673469  135540.0   2.0   \n",
       "08:20.5     135530.0   13.0   1761980.0  135536.923077  135530.0   4.0   \n",
       "\n",
       "                ap1   av1       bp2   bv2  ...   bv4       ap4   av4  \\\n",
       "Timestamp                                  ...                         \n",
       "00:00.5    135190.0   2.0  135130.0   9.0  ...   6.0  135300.0  15.0   \n",
       "00:01.0    135280.0   8.0  135220.0   1.0  ...   1.0  135310.0   1.0   \n",
       "00:01.5    135230.0   6.0  135200.0   2.0  ...   3.0  135270.0  19.0   \n",
       "00:02.0    135220.0   3.0  135180.0   1.0  ...   4.0  135250.0   2.0   \n",
       "00:02.5    135160.0   1.0  135120.0   3.0  ...  11.0  135210.0   2.0   \n",
       "...             ...   ...       ...   ...  ...   ...       ...   ...   \n",
       "08:18.5    135520.0   3.0  135490.0   7.0  ...   8.0  135550.0   9.0   \n",
       "08:19.0    135510.0   1.0  135490.0   7.0  ...   8.0  135540.0  10.0   \n",
       "08:19.5    135530.0  12.0  135500.0  25.0  ...   5.0  135560.0   4.0   \n",
       "08:20.0    135570.0  28.0  135530.0   5.0  ...  19.0  135600.0  66.0   \n",
       "08:20.5    135560.0   4.0  135520.0  20.0  ...  25.0  135590.0   9.0   \n",
       "\n",
       "                bp5   bv5       ap5   av5  ret_value10  ret_value20  \\\n",
       "Timestamp                                                             \n",
       "00:00.5    135070.0   1.0  135310.0   1.0     0.000000     0.000000   \n",
       "00:01.0    135190.0   7.0  135320.0   3.0     0.000000     0.000000   \n",
       "00:01.5    135170.0   1.0  135280.0  12.0     0.000000     0.000000   \n",
       "00:02.0    135140.0   1.0  135260.0   6.0     0.000000     0.000000   \n",
       "00:02.5    135070.0   2.0  135220.0   5.0     0.000000     0.000000   \n",
       "...             ...   ...       ...   ...          ...          ...   \n",
       "08:18.5    135460.0   9.0  135560.0   4.0     0.000148     0.000074   \n",
       "08:19.0    135460.0   9.0  135550.0   9.0     0.000074     0.000000   \n",
       "08:19.5    135470.0   8.0  135570.0  28.0     0.000221     0.000000   \n",
       "08:20.0    135500.0  25.0  135610.0   6.0     0.000295     0.000590   \n",
       "08:20.5    135490.0   9.0  135600.0  66.0     0.000148     0.000295   \n",
       "\n",
       "           ret_value30  \n",
       "Timestamp               \n",
       "00:00.5       0.000000  \n",
       "00:01.0       0.000000  \n",
       "00:01.5       0.000000  \n",
       "00:02.0       0.000000  \n",
       "00:02.5       0.000000  \n",
       "...                ...  \n",
       "08:18.5      -0.000148  \n",
       "08:19.0      -0.000295  \n",
       "08:19.5      -0.000221  \n",
       "08:20.0       0.000221  \n",
       "08:20.5       0.000074  \n",
       "\n",
       "[1000 rows x 27 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp_arr_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-42-3778e9cfb272>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtemp_arr_data\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"ret_value10\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"ret_value20\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"ret_value30\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcorr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtemp_arr_data\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"ap5\"\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mempty\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36mcorr\u001b[1;34m(self, method, min_periods)\u001b[0m\n\u001b[0;32m   7463\u001b[0m         \u001b[0mmat\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnumeric_df\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   7464\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 7465\u001b[1;33m         \u001b[1;32mif\u001b[0m \u001b[0mmethod\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"pearson\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   7466\u001b[0m             \u001b[0mcorrel\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlibalgos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnancorr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mensure_float64\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mminp\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmin_periods\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   7467\u001b[0m         \u001b[1;32melif\u001b[0m \u001b[0mmethod\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"spearman\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\generic.py\u001b[0m in \u001b[0;36m__nonzero__\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m   1476\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1477\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__nonzero__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1478\u001b[1;33m         raise ValueError(\n\u001b[0m\u001b[0;32m   1479\u001b[0m             \u001b[1;34mf\"The truth value of a {type(self).__name__} is ambiguous. \"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1480\u001b[0m             \u001b[1;34m\"Use a.empty, a.bool(), a.item(), a.any() or a.all().\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
     ]
    }
   ],
   "source": [
    "temp_arr_data[[\"ret_value10\", \"ret_value20\", \"ret_value30\"]].corr(temp_arr_data[\"ap5\"]).empty()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
